package UI;

/**
 *
 * @author JASMINE
 */


import javax.swing.GroupLayout.Alignment;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class ASummaryOfCases extends javax.swing.JFrame
{
    DefaultTableModel model;
    JTable table;

    public ASummaryOfCases()
    {
        initDUI();
    }

     public static void main(String args[])
     {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
        new ASummaryOfCases().setVisible(true);
            }
        });
     }


     private void initDUI()
     {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Summary of Cases");

        GroupLayout.SequentialGroup seqGroup;
        GroupLayout.ParallelGroup parGroup;
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);

        seqGroup = layout.createSequentialGroup();
        parGroup = layout.createParallelGroup(GroupLayout.Alignment.CENTER);    

        //MainPanel
        JPanel mainPanel = new JPanel();
        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Summary of Cases"));
        mainPanel.setPreferredSize(new java.awt.Dimension(800, 420));
        mainPanel.setLayout(new java.awt.GridLayout());

        //AllCasesPanel
        JPanel allCpanel = new JPanel();
        allCpanel.setBorder(javax.swing.BorderFactory.createTitledBorder("All Cases"));
        allCpanel.setLayout(new java.awt.GridLayout());
        createTable();
        JScrollPane scrollPane = new JScrollPane(table);
        allCpanel.add(scrollPane);
        mainPanel.add(allCpanel);

        //AssignedCasesPanel
        JPanel assignedCpanel = new JPanel();
        assignedCpanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Assigned Cases"));
        assignedCpanel.setLayout(new java.awt.GridLayout());
        createTable();
        JScrollPane scrollPane1 = new JScrollPane(table);
        assignedCpanel.add(scrollPane1);
        mainPanel.add(assignedCpanel);

        //OutstandingCasesPanel
        JPanel outstandingCpanel = new JPanel();
        outstandingCpanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Outstanding Cases"));
        outstandingCpanel.setLayout(new java.awt.GridLayout());
        createTableWithBtn();
        JScrollPane scrollPane2 = new JScrollPane(table);
        outstandingCpanel.add(scrollPane2);
        mainPanel.add(outstandingCpanel);


        seqGroup.addGroup(layout.createSequentialGroup()
                .addComponent(mainPanel));
        parGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
                .addComponent(mainPanel));


        layout.setHorizontalGroup(seqGroup);
        layout.setVerticalGroup(parGroup);
        
        pack();
     }

     private void createTable()
     {
        //String[] columnNames = {"Date", "String", "Integer", "Decimal", ""};
        String[] columnNames = {"Case", "Handled By"};
        Object[][] data =
        {
            {"A", "123"},
            {"B", "222", },
            {"C", "333"}
        };

        model = new DefaultTableModel(data, columnNames);
        table = new JTable( model )
        {
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                if(vColIndex == 2) return true;
                else return false;
            }
        };
    }

     private void createTableWithBtn()
     {
        //String[] columnNames = {"Date", "String", "Integer", "Decimal", ""};
        String[] columnNames = {"Case", "Status", ""};
        Object[][] data =
        {
            {"A", "Done", "Modify"},
            {"B", "Done", "Modify"},
            {"C", "Assigned", "Modify"}

            /*
            {new Date(), "A", new Integer(1), new Double(5.1), "Delete0"},
            {new Date(), "B", new Integer(2), new Double(6.2), "Delete1"},
            {new Date(), "C", new Integer(3), new Double(7.3), "Delete2"},
            {new Date(), "D", new Integer(4), new Double(8.4), "Delete3"}
             *
             */
        };

        model = new DefaultTableModel(data, columnNames);
        table = new JTable( model )
        {
            public boolean isCellEditable(int rowIndex, int vColIndex) 
            {   
                if(vColIndex == 2) return true;
                else return false;
            }
        };
        //Create button column (2 = displayed 3 rows)
        ButtonColumn buttonColumn = new ButtonColumn(table, 2);
    }


   class ButtonColumn extends AbstractCellEditor
        implements TableCellRenderer, TableCellEditor, ActionListener
    {
        JTable table;
        JButton renderButton;
        JButton editButton;
        String text;

        public ButtonColumn(JTable table, int column)
        {
            super();
            this.table = table;
            renderButton = new JButton();

            editButton = new JButton();
            editButton.setFocusPainted( false );
            editButton.addActionListener( this );

            TableColumnModel columnModel = table.getColumnModel();
            columnModel.getColumn(column).setCellRenderer( this );
            columnModel.getColumn(column).setCellEditor( this );
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            if (hasFocus)
            {
                renderButton.setForeground(table.getForeground());
                renderButton.setBackground(UIManager.getColor("Button.background"));
            }
            else if (isSelected)
            {
                renderButton.setForeground(table.getSelectionForeground());
                 renderButton.setBackground(table.getSelectionBackground());
            }
            else
            {
                renderButton.setForeground(table.getForeground());
                renderButton.setBackground(UIManager.getColor("Button.background"));
            }

            renderButton.setText( (value == null) ? "" : value.toString() );
            return renderButton;
        }

        public Component getTableCellEditorComponent(
            JTable table, Object value, boolean isSelected, int row, int column)
        {
            text = (value == null) ? "" : value.toString();
            editButton.setText( text );
            return editButton;
        }

        public Object getCellEditorValue()
        {
            return text;
        }

        public void actionPerformed(ActionEvent e)
        {
            fireEditingStopped();
            BCaseInfoModify BcaseUI = new BCaseInfoModify();
            System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
            

        }
    }



}
